package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.WarehouseMapper;
import com.indusfo.spc.pojo.Warehouse;
import com.indusfo.spc.service.WarehouseService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    private  static  final Logger logger = LoggerFactory.getLogger(WarehouseServiceImpl.class);

    @Autowired
    private WarehouseMapper warehouseMapper;


    @Override
    public JSONObject selectWarehouse(Warehouse warehouse) {
        List<Warehouse> warehouseList = null;
        int count = 0;
        try {
            Integer pagesize = warehouse.getPagesize();
            Integer pageindex = warehouse.getPageindex();
            if(pagesize != null && pageindex != null) {
                warehouse.setIncept(pagesize*(pageindex - 1));
            }
            warehouseList = warehouseMapper.selectWarehouse(warehouse);
            //查询分页总记录数
            count = warehouseMapper.countWarehouse(warehouse); //返回查询到的总记录数
            if (warehouseList.isEmpty()) {
                return JSONObject.oK("没有查询到相关数据", warehouseList, count);
            }
            return JSONObject.oK("查询成功", warehouseList, count);
        } catch (
        GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject insertWarehouse(Warehouse warehouse) {
        try {
            if(warehouse.getlWarehouse()!= null) {
                throw new ParamsErrorException("新增时，不能填写id");
            }
            checkParam(warehouse);
            // 调用存储过程
            int row = warehouseMapper.insertSelective(warehouse);
            if(row == 0) {
                throw new ModifyFailedException("数据新增失败");
            }
            return JSONObject.oK("新增成功");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject updateWarehouse(Warehouse warehouse) {
        try {
            if(warehouse.getlWarehouse()== null) {
                throw new ParamsErrorException("请选择要更新的仓库");
            }
            checkParam(warehouse);
            // 调用存储过程
            int row = warehouseMapper.updateByPrimaryKeySelective(warehouse);
            if(row == 0) {
                throw new ModifyFailedException("数据更新失败！");
            }
            return JSONObject.oK("更新成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    @Override
    public JSONObject definesWarehouse(Long[] warehouseIds, Integer dataState) {
        try {
            if(dataState == null) {
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if (warehouseIds == null) {
                throw new ParamsErrorException("id不能为空");
            }
            // 执行存储过程
            int row = warehouseMapper.definesWarehouse(warehouseIds, dataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                }
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 参数校验
     */
    private void checkParam(Warehouse warehouse) {
//        if (bldc.getBldctypeId() == null) {
//            throw new ParamsErrorException("不良对策类型不能为空");
//        }
        if (StringUtils.isEmpty(warehouse.getVcWarehouseCode())) {
            throw new ParamsErrorException("请填写仓库编码");
        }
        if (warehouse.getVcWarehouseCode().getBytes().length > 50) {
            throw new ParamsErrorException("仓库编码超过规定,长度50");
        }
        if (StringUtils.isEmpty(warehouse.getVcWarehouseName())) {
            throw new ParamsErrorException("请填写仓库名称");
        }
        if (warehouse.getVcWarehouseName().getBytes().length > 50) {
            throw new ParamsErrorException("仓库名称超过规定,长度50");
        }
        // 编码不能重复
        Integer depCounts = warehouseMapper.queryWarehouse(warehouse);
        if (depCounts !=0) {
            throw new ParamsErrorException("该仓库编码已存在");
        }
    }

}
